package hibernate.web;

import hibernate.fileControler.CheckFileHash;
import hibernate.fileControler.MyException;
import hibernate.model.Aygit;
import hibernate.model.Resimler;
import hibernate.model.Sirketprofil;
import hibernate.model.Telefon;
import hibernate.service.EmmyService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("TelefonBean")
@Scope("session")
public class TelefonBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;

	private Telefon selectedTelefon;
	private List<Telefon> telefonList;
	private List<Resimler> selectedTlfResimList;
	private Resimler selectedTlfResim;

	private SirketprofilBean firmaBean;
	private ResimlerBean resimBean;
	private ArrayList<UploadedFile> fileList = new ArrayList<UploadedFile>();

	@Override
	@PostConstruct
	public void init() {
	}

	@Override
	public String restart() {
		init();
		return "";
	}

	@Override
	public String deleteView() {
		emmyService.delete(selectedTelefon);
		init();
		return "";
	}

	@Override
	public String viewNew() {
		System.out.println("Telefon viewNew");
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean());
		agtBean.setYeniUrun(true);
		Sirketprofil srf = ((SirketprofilBean) emmyService
				.getBean(new SirketprofilBean())).getCurrentSirketpro();
		Aygit agt = new Aygit(srf, "TELEFON", "", "");

		selectedTelefon = new Telefon();
		selectedTelefon.setAygit(agt);
		selectedTelefon.setSirketprofil(srf);
		agtBean.setSelectedAygitObject(selectedTelefon);
		agtBean.setEdit(true);
		return "";
	}

	@Override
	public String viewDetail() {/*
								 * List<Telefon> valueList = (List<Telefon>)
								 * telefonTable.getValue();
								 * System.out.println("table size : "
								 * +valueList.size()); Iterator<SimpleRowKey>
								 * keys = telefonTable.getSelection().getKeys();
								 * 
								 * if(keys.hasNext()){ currentTelefon=
								 * valueList.get(keys.next().intValue());
								 * System.
								 * out.println("dolu = "+currentTelefon.getAygit
								 * ()); return "detail"; }else{
								 * System.out.println("bo�ta"); return null; }
								 */
		return "detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean());
		agtBean.setSelectedAygitObject(selectedTelefon);
		resimBean = (ResimlerBean) emmyService.getBean(new ResimlerBean());
		try {
			if (resimBean.SaveOrUpdate().equals("OK")) {
				fileList = resimBean.getFileList();
				Set<Resimler> resCollec = new HashSet<Resimler>();
				for (int i = 0; i < fileList.size(); i++) {
					Resimler res = new Resimler();
					res.setResimKonum("../productImages/Telefon/"
							+ (CheckFileHash.fileCheck(fileList.get(i)) + fileList
									.get(i).getFileName()).toLowerCase());
					res.setAygit(selectedTelefon.getAygit());
					resCollec.add(res);
					System.out.println(resCollec.size());
				}
				selectedTelefon.getAygit().setResimlers(resCollec);
				emmyService.saveOrUpdate(selectedTelefon);
				agtBean.deleteSelectedAygit();
				emmyService.clearSession(resimBean);
			}
		} catch (Exception e) {
			System.out.println("Telefon Save error : " + e);
		}
		return "";
	}

	@SuppressWarnings("unchecked")
	public String getDetayliTelefon() {
		telefonList = (List<Telefon>) emmyService.getQuery(createQuery());
		return "list";
	}

	@Override
	public String createQuery() {
		Telefon tel = selectedTelefon;
		String query = "from Telefon";

		if (tel == null) {
			return query;
		}
		if (tel.getSirketprofil().getFirmaid() != 0) {
			if (query.indexOf("where") != -1) {
				query += " and ureticiid=" + tel.getSirketprofil().getFirmaid()
						+ "";

			} else {
				query += " where ureticiid="
						+ tel.getSirketprofil().getFirmaid() + "";
			}
		}
		if (tel.getBluetooth() != null) {
			if (query.indexOf("where") != -1) {
				query += " and bluetooth like '%" + tel.getBluetooth() + "%'";

			} else {
				query += " where bluetooth like '%" + tel.getBluetooth() + "%'";
			}
		}
		if (tel.getCiftSim() != null) {
			if (query.indexOf("where") != -1) {
				query += " and ciftSim like '%" + tel.getCiftSim() + "%'";

			} else {
				query += " where ciftSim like '%" + tel.getCiftSim() + "%'";
			}
		}
		if (tel.getWifi() != null) {
			if (query.indexOf("where") != -1) {
				query += " and wifi like '%" + tel.getWifi() + "%'";

			} else {
				query += " where wifi like '%" + tel.getWifi() + "%'";
			}
		}
		if (tel.getHariciHafizaDestek() != null) {
			if (query.indexOf("where") != -1) {
				query += " and hariciHafizaDestek like '%"
						+ tel.getHariciHafizaDestek() + "%'";

			} else {
				query += " where hariciHafizaDestek like '%"
						+ tel.getHariciHafizaDestek() + "%'";
			}
		}
		if (tel.getGps() != null) {
			if (query.indexOf("where") != -1) {
				query += " and gps like '%" + tel.getGps() + "%'";

			} else {
				query += " where gps like '%" + tel.getGps() + "%'";
			}
		}
		return query;
	}

	@Override
	public void getSession() {
		// TODO Auto-generated method stub

	}

	public Telefon getSelectedTelefon() {
		return selectedTelefon;
	}

	public void setSelectedTelefon(Telefon selectedTelefon) {
		this.selectedTelefon = selectedTelefon;
	}

	public List<Telefon> getTelefonList() {
		return telefonList;
	}

	public void setTelefonList(List<Telefon> telefonList) {
		this.telefonList = telefonList;
	}

	public List<Resimler> getSelectedTlfResimList() {
		return selectedTlfResimList;
	}

	public void setSelectedTlfResimList(List<Resimler> selectedTlfResimList) {
		this.selectedTlfResimList = selectedTlfResimList;
	}

	public Resimler getSelectedTlfResim() {
		return selectedTlfResim;
	}

	public void setSelectedTlfResim(Resimler selectedTlfResim) {
		this.selectedTlfResim = selectedTlfResim;
	}

	public SirketprofilBean getFirmaBean() {
		return firmaBean;
	}

	public void setFirmaBean(SirketprofilBean firmaBean) {
		this.firmaBean = firmaBean;
	}

	public ResimlerBean getResimBean() {
		return resimBean;
	}

	public void setResimBean(ResimlerBean resimBean) {
		this.resimBean = resimBean;
	}

	public ArrayList<UploadedFile> getFileList() {
		return fileList;
	}

	public void setFileList(ArrayList<UploadedFile> fileList) {
		this.fileList = fileList;
	}
}